# 定义输入值和期望输出
x_1 = 40.0
x_2 = 80.0
expected_output = 60.0

# 初始化

# 定义权重
w_1_11 = 0.5
w_1_12 = 0.5
w_1_13 = 0.5

w_1_21 = 0.5
w_1_22 = 0.5
w_1_23 = 0.5

w_2_11 = 1.0
w_2_21 = 1.0
w_2_31 = 1.0

# 前向传播
z_1 = x_1 * w_1_11 + x_2 * w_1_12
z_2 = x_1 * w_1_12 + x_2 * w_1_22
z_3 = x_1 * w_1_13 + x_2 * w_1_23

y_pred = z_1 * w_2_11 + z_2 * w_2_21 + z_3 * w_2_31

print(f" 前向传播预测值{y_pred}")

# 计算损失值（L2 损失）
loss = 0.5 * (expected_output - y_pred) ** 2
print(f"当前Loss 值为：{loss}")

# 开始计算梯度
# 计算输出层关于损失函数的梯度

d_loss_predictied_output = -(expected_output - y_pred)

# 计算权重关于损失函数的梯度
d_loss_w_2_11 = d_loss_predictied_output * z_1
d_loss_w_2_21 = d_loss_predictied_output * z_2
d_loss_w_2_31 = d_loss_predictied_output * z_3

d_loss_w_1_11 = d_loss_predictied_output * w_2_11 * x_1
d_loss_w_1_21 = d_loss_predictied_output * w_2_11 * x_2
d_loss_w_1_12 = d_loss_predictied_output * w_2_21 * x_1
d_loss_w_1_22 = d_loss_predictied_output * w_2_21 * x_2
d_loss_w_1_13 = d_loss_predictied_output * w_2_31 * x_1
d_loss_w_1_23 = d_loss_predictied_output * w_2_31 * x_2

# 使用梯度下降法更新权重
learning_rate = 1e-5
w_2_11 -= learning_rate * d_loss_w_2_11
w_2_21 -= learning_rate * d_loss_w_2_21
w_2_31 -= learning_rate * d_loss_w_2_31

w_1_11 -= learning_rate * d_loss_w_1_11
w_1_12 -= learning_rate * d_loss_w_1_12
w_1_13 -= learning_rate * d_loss_w_1_13
w_1_21 -= learning_rate * d_loss_w_1_21
w_1_22 -= learning_rate * d_loss_w_1_22
w_1_23 -= learning_rate * d_loss_w_1_23

# 前向传播
z_1 = x_1 * w_1_11 + x_2 * w_1_21
z_2 = x_1 * w_1_12 + x_2 * w_1_22
z_3 = x_1 * w_1_13 + x_2 * w_1_23
y_pred = z_1 * w_2_11 + z_2 * w_2_21 + z_3 * w_2_31

print(f"前向传播预测值为：{y_pred}")

# 计算损失 （L2 损失）
loss = 0.5 * (expected_output - y_pred) ** 2
print(f"当前Loss值:{loss}")
